package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Role;
import com.itheima.pojo.User;
import com.itheima.service.UserService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.security.Principal;
import java.util.List;

@RestController
@RequestMapping("user")
public class UserController {

    @Reference
    private UserService userService;

    @RequestMapping("getUsername")
    public Result getUsername(Principal principal){
        try {
            String username = principal.getName();
            return new Result(true, MessageConstant.GET_USERNAME_SUCCESS,username);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.GET_USERNAME_FAIL);
        }
    }

    /**
     * 用户管理分页查询
     *
     * @return
     */
    @RequestMapping("findPage")
    public Result findPage(@RequestBody QueryPageBean queryPageBean) {
        try {
            PageResult pageResult = userService.findPage(queryPageBean);
            return new Result(true, "查询用户信息成功", pageResult);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "查询用户信息失败");
        }
    }

    /**
     * 删除用户
     * @param id
     * @return
     */
    @RequestMapping("deleteById")
    public Result deleteById(@RequestParam("id") Integer id) {
        try {
            userService.deleteById(id);
            return new Result(true, "删除用户成功");
        } catch (RuntimeException e) {
            return new Result(false, e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "删除用户失败");
        }
    }

    /**
     * 添加用户
     * @param user
     * @return
     */
    @RequestMapping("add")
    public Result add(@RequestParam("checkitemIds") Integer[] checkitemIds,@RequestBody User user) {
        try {
            userService.add(user,checkitemIds);
            return new Result(true, "添加用户成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, e.getMessage());
        }
    }

    /**
     * 显示添加权限
     * @return
     */
    @RequestMapping("findRole")
    public Result findRole() {
        try {
            List<Role> role = userService.findRole();
            return new Result(true, "显示权限成功", role);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "显示权限失败");
        }
    }

    /**
     * 回显数据
     * @param id
     * @return
     */
    @RequestMapping("findById")
    public Result findById(@RequestParam("id") Integer id) {
        try {
            User user = userService.findById(id);
            return new Result(true, "回显数据成功", user);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "回显数据失败");
        }
    }

    /**
     * 回显数据
     * @param id
     * @return
     */
    @RequestMapping("findRoleAndUserById")
    public Result findRoleAndUserById(@RequestParam("id") Integer id) {
        try {
            List<Integer> roleId = userService.findRoleAndUserById(id);
            return new Result(true, "回显数据成功", roleId);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "回显数据失败");
        }
    }

    /**
     * 编辑修改
     * @return
     */
    @RequestMapping("edit")
    public Result edit(@RequestParam("checkitemIds") Integer[] checkitemIds,@RequestBody User user) {
        try {
            userService.edit(user,checkitemIds);
            return new Result(true, "修改用户成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "修改用户失败");
        }
    }


}
